<?php

if (GetTariff($GL, $_SESSION['LOGIN_UID']) != "0") {
    $innerHTML = "
<td colspan=4>
    <object type=\"application/x-shockwave-flash\" data=\"/swf/player_mp3_maxi.swf\" width=\"400\" height=\"20\">
        <param name=\"movie\" value=\"/swf/player_mp3_maxi.swf\" />
        <param name=\"bgcolor\" value=\"#999999\" />
        <param name=\"FlashVars\" value=\"mp3='+curSongMp3+'&amp;width=400&amp;autoplay=1&amp;showstop=1&amp;showvolume=1&amp;buttonwidth=30&amp;volumewidth=60&amp;volumeheight=10&amp;loadingcolor=999999&amp;bgcolor=999999&amp;bgcolor1=999999&amp;bgcolor2=999999&amp;slidercolor2=cccccc&amp;sliderovercolor=777777&amp;buttonovercolor=777777\" />
    </object>
</td>";

    $img_order = 1;
    $SCRIPTS .= "
<script type=text/javascript>
    function PlayerStart(curSongId, curSongMp3) {
        if (document.getElementById('Player')) {
            var prevPlayer = document.getElementById('Player');

            var prevImg = prevPlayer.previousSibling.getElementsByTagName('img')[".$img_order."];
            prevImg.setAttribute('src', \"/images/start.svg\");

            var prevOnclick = prevImg.getAttribute('onclick');
            prevImg.setAttribute('onclick', prevOnclick.replace('PlayerStop', 'PlayerStart'));

            prevPlayer.parentNode.removeChild(prevPlayer);
        }

        var newPlayer = document.createElement('tr');
        newPlayer.id = 'Player';
        newPlayer.innerHTML = '".$innerHTML."';

        var curSong = document.getElementById(curSongId);
        curSong.parentNode.insertBefore(newPlayer, curSong.nextSibling);
        
        var curImg = curSong.getElementsByTagName('img')[".$img_order."];
        curImg.setAttribute('src', \"/images/stop.svg\");
        curImg.setAttribute('onclick', \"PlayerStop('\"+curSongId+\"','\"+curSongMp3+\"')\");
    }
    function PlayerStop(curSongId, curSongMp3) {
        if (document.getElementById('Player')) {
            var prevPlayer = document.getElementById('Player');
            prevPlayer.parentNode.removeChild(prevPlayer);
        }

        var curSong = document.getElementById(curSongId);
        var curImg = curSong.getElementsByTagName('img')[".$img_order."];
        curImg.setAttribute('src', \"/images/start.svg\");
        curImg.setAttribute('onclick', \"PlayerStart('\"+curSongId+\"','\"+curSongMp3+\"')\");
    }
</script>";

    $conn_id = ftp_connect("localhost");
    if (!@ftp_login($conn_id, $_SESSION['LOGIN_NAME'], $_SESSION['LOGIN_PASS']))
        $t_BODY = "<p>Возникла непредвиденная ошибка.</p><br />";
    else {
        $i = 0;
        $t_BODY = "
<fieldset>
    <legend>/music</legend>
    <div class=\"list music\">
        <table id=\"listMusic\">";
        $contents = ftp_nlist($conn_id, "/music");
        foreach ($contents as $f) { 
            if(preg_match("/\.mp3$/i", $f)) {
                $f = iconv('CP1251', 'UTF-8', $f);
                $t_BODY .= "
        <tr id=\"song".$i."\">
            <td class=\"left\">".pcgbasename($f)."</td>
            <td class=\"right\">
                <a target=\"_blank\" href=\"/download.php?tmp=".base64_encode("music/".$_SESSION['LOGIN_NAME']."/music")."&amp;music=".pcgbasename($f)."\"><img class=\"user\" src=\"/images/download.svg\" alt=\"Скачать\" /></a>
            </td>
            <td class=\"right\">
                <img onclick=\"PlayerStart('song".$i."','/music/".$_SESSION['LOGIN_NAME'].$f."')\" class=\"user\" src=\"/images/start.svg\" alt=\"Играть\" />
            </td>
            <td class=\"right\">
                <a href=\"/?action=del&amp;dir=music&amp;music=".pcgbasename($f)."\"><img class=\"user\" src=\"/images/delete.svg\" alt=\"Удалить\" /></a>
            </td>
        </tr>";
                $i++;
            }
        }
        $t_BODY .= "
        </table>
    </div>
</fieldset>
<fieldset>
    <legend>/Эфирная заставка  (Вывод после 3х муз. файлов)</legend>
    <div class=\"list jingls\">
        <table id=\"listMusic\">";
        $contents = ftp_nlist($conn_id, "/jingls");
        foreach ($contents as $f) { 
            if(preg_match("/\.mp3$/i", $f)) {
                $f = iconv('CP1251', 'UTF-8', $f);
                $t_BODY .= "
        <tr id=\"song".$i."\">
            <td class=\"left\">".pcgbasename($f)."</td>
            <td class=\"right\">
                <a target=\"_blank\" href=\"/download.php?tmp=".base64_encode("music/".$_SESSION['LOGIN_NAME']."/jingls")."&amp;music=".pcgbasename($f)."\"><img class=\"user\" src=\"/images/download.svg\" alt=\"Скачать\" /></a>
            </td>
            <td class=\"right\">
                <img onclick=\"PlayerStart('song".$i."','/music/".$_SESSION['LOGIN_NAME'].$f."')\" class=\"user\" src=\"/images/start.svg\" alt=\"Играть\" />
            </td>
            <td class=\"right\">
                <a href=\"/?action=del&amp;dir=jingls&amp;music=".pcgbasename($f)."\"><img class=\"user\" src=\"/images/delete.svg\" alt=\"Удалить\" /></a>
            </td>
        </tr>";
                $i++;
            }
        }
        $t_BODY .= "
        </table>
    </div>
</fieldset>";

        ftp_close($conn_id);
    }
}
else
    $t_BODY = "<p>В Ваш тариф не входит доступ к ftp.</p>";

$t_title = "Список файлов";
$HEADER .= " - ".$t_title;

?>
